package myfs

import (
	"context"
	"syscall"

	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"
)

type DirInode struct {
	fs.Inode
	idx          uint64
	newFileInode func([]byte) fs.InodeEmbedder
}

func NewDirInode(newFileInode func([]byte) fs.InodeEmbedder) *DirInode {
	return &DirInode{
		newFileInode: newFileInode,
	}
}

// 保存文件
func (sf *DirInode) SaveFile(fileName string, data []byte, force bool) bool {
	sf.idx++
	ch := sf.NewPersistentInode(
		context.Background(),
		sf.newFileInode(data),
		fs.StableAttr{Ino: sf.idx},
	)

	return sf.AddChild(fileName, ch, force)
}

func (sf *DirInode) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {

	out.Mode = 0755
	return 0
}

var _ = (fs.NodeGetattrer)((*DirInode)(nil))
